package com.ruoyi.biz.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.biz.system.domain.BizArea;
import com.ruoyi.biz.system.service.IBizAreaService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 区域Controller
 *
 * @author Fei.Cong
 * @date 2023-05-29
 */
@RestController
@RequestMapping("/system/area" )
public class BizAreaController extends BaseController {

    @Autowired
    private IBizAreaService bizAreaService;

    /**
     * 查询区域列表
     */
    @PreAuthorize("@ss.hasPermi('system:area:list')" )
    @GetMapping("/list" )
    public AjaxResult list(BizArea bizArea) {
        LambdaQueryWrapper<BizArea> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return success(bizAreaService.list(lambdaQueryWrapper));
    }

    /**
     * 分页查询区域列表
     */
    @PreAuthorize("@ss.hasPermi('system:area:list')" )
    @GetMapping("/page" )
    public TableDataInfo page(BizArea bizArea) {
        return getDataTable(bizAreaService.pageList(bizArea, startPage()));
    }

    /**
     * 导出区域列表
     */
    @PreAuthorize("@ss.hasPermi('system:area:export')" )
    @Log(title = "区域" , businessType = BusinessType.EXPORT)
    @PostMapping("/export" )
    public void export(HttpServletResponse response, BizArea bizArea) {
        LambdaQueryWrapper<BizArea> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<BizArea> list = bizAreaService.list(lambdaQueryWrapper);
        ExcelUtil<BizArea> util = new ExcelUtil<BizArea>(BizArea. class);
        util.exportExcel(response, list, "区域数据" );
    }

    /**
     * 获取区域详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:area:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return success(bizAreaService.getById(id));
    }

    /**
     * 新增区域
     */
    @PreAuthorize("@ss.hasPermi('system:area:add')" )
    @Log(title = "区域" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizArea bizArea) {
        return toAjax(bizAreaService.save(bizArea));
    }

    /**
     * 修改区域
     */
    @PreAuthorize("@ss.hasPermi('system:area:edit')" )
    @Log(title = "区域" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizArea bizArea) {
        return toAjax(bizAreaService.updateById(bizArea));
    }

    /**
     * 删除区域
     */
    @PreAuthorize("@ss.hasPermi('system:area:remove')" )
    @Log(title = "区域" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(bizAreaService.removeBatchByIds(ids));
    }
}
